/**
 * File Name : AndroidGame.java
 * <p>
 * Author : maidou
 * <p>
 * Create Time : 2012-11-18, 下午10:38:33
 * <p>
 */

package com.ttomato.game.framework.impl;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

import com.ttomato.game.framework.Audio;
import com.ttomato.game.framework.FileIO;
import com.ttomato.game.framework.Game;
import com.ttomato.game.framework.Graphics;
import com.ttomato.game.framework.Input;
import com.ttomato.game.framework.Screen;

/**
 * Add the description for the file here.
 * <p>
 */
public abstract class AndroidGame extends Activity implements Game
{
    AndroidFastRenderView mRenderView;
    
    Graphics mGraphics;
    
    Audio mAudio;
    
    Input mInput;
    
    FileIO mFileIO;
    
    Screen mScreen;
    
    WakeLock mWakeLock;
    
    @Override
    public Input getInput()
    {
        return mInput;
    }
    
    @Override
    public FileIO getFileIO()
    {
        return mFileIO;
    }
    
    @Override
    public Graphics getGraphics()
    {
        return mGraphics;
    }
    
    @Override
    public Audio getAudio()
    {
        return mAudio;
    }
    
    @Override
    public void setScreen(Screen screen)
    {
        if (null == screen)
        {
            throw new IllegalArgumentException("Screen must not be null!!");
        }
        
        mScreen.pause();
        mScreen.dispose();
        screen.resume();
        screen.update(0);
        mScreen = screen;
    }
    
    @Override
    public Screen getCurrentScreen()
    {
        return mScreen;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
        
        int frameBufferWidth = isLandscape ? 480 : 320;
        int frameBufferHeight = isLandscape ? 320 : 480;
        
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight,
                Config.RGB_565);
        
        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();
        
        mRenderView = new AndroidFastRenderView(this, frameBuffer);
        mGraphics = new AndroidGraphics(getAssets(), frameBuffer);
        mFileIO = new AndroidFileIO(getAssets());
        mAudio = new AndroidAudio(this);
        mInput = new AndroidInput(this, mRenderView, scaleX, scaleY);
        mScreen = getStartScreen();
        setContentView(mRenderView);
        
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
                "GLGame");
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        mWakeLock.acquire();
        mScreen.resume();
        mRenderView.resume();
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        mWakeLock.release();
        mRenderView.pause();
        mScreen.pause();
        
        if (isFinishing())
        {
            mScreen.dispose();
        }
    }
    
}
